<?php


namespace app\admin\controller;


use think\Cache;
use think\exception\ValidateException;
use think\facade\Config;
use think\facade\Filesystem;

class Ajax extends Base
{
    protected $noNeedRight = ['*'];

    /**
     * 清除缓存
     */
    public function cacheClear()
    {
        $type = $this->request->request("type");
        $runtimePath = $this->app->getRuntimePath();
        switch ($type) {
            case 'all':
            case 'content':
                rmdirs($runtimePath, true);
                Cache::clear();
                if ($type == 'content') {
                    break;
                }
            case 'template':
                $runtimePath .= 'temp' . DIRECTORY_SEPARATOR;
                rmdirs($runtimePath, true);
                if ($type == 'template') {
                    break;
                }
        }
        $this->success('缓存清除成功');
    }

    public function upload()
    {
        $file = $this->request->file('file');
        $response = ['code' => 0, 'msg' => '网络异常', 'data' => []];
        if (empty($file)) {
            $response['msg'] = '没有文件上传或服务器上传限制';
            return json($response, 200);
        }
        $upload = Config::get('upload');
        preg_match('/(\d+)(\w+)/', $upload['maxsize'], $matches);
        $type = strtolower($matches[2]);
        $typeDict = ['b' => 0, 'k' => 1, 'kb' => 1, 'm' => 2, 'mb' => 2, 'gb' => 3, 'g' => 3];
        $size = (int)$upload['maxsize'] * pow(1024, isset($typeDict[$type]) ? $typeDict[$type] : 0);
        $extensions = $upload['extensions'] ?? '';
        //文件后缀
        $suffix = $file->getOriginalExtension();
        $suffix = $suffix && preg_match("/^[a-zA-Z0-9]+$/", $suffix) ? $suffix : 'file';
        $mimeType = $file->getOriginalMime();
        $fileSize = $file->getSize();//未知
        //禁止上传PHP和HTML文件
        if (in_array($mimeType, ['text/x-php', 'text/html']) || in_array($suffix, ['php', 'html', 'htm'])) {
            $response['msg'] = '上传的文件格式受到限制';
            return json($response, 200);
        }
        $filesystem = Config::get('filesystem');
        $rootPath = $filesystem['disks']['webupload']['url'];
        //验证文件后缀
        if ($extensions !== '*' && !in_array($suffix, explode(',', $extensions))) {
            $response['msg'] = '上传的文件格式受到限制';
            return json($response, 200);
        }
        try {
            $savename = Filesystem::disk('webupload')->putFile('', $file);
        } catch (ValidateException $e) {
            $response['msg'] = $e->getMessage();
            return json($response, 200);
        }
        $response['msg'] = '上传成功';
        $response['code'] = 1;
        $savePath = $rootPath . '/' . $savename;
        $response['data'] = ['save_name' => $savePath];
        return json($response);
    }
    /**
     * 生成后缀图标
     */
    public function icon()
    {
        $suffix = $this->request->request("suffix");
        header('Article-type: image/svg+xml');
        $suffix = $suffix ? $suffix : "FILE";
        echo build_suffix_image($suffix);
        exit;
    }

    public function editorUpload(){
        $file = $this->request->file('imgFile');
        $dir = $this->request->param('dir');
        if(empty($dir)){
            $dir ='file';
        }
        $response = ['error' => 1, 'message' => '网络异常'];
        if (empty($file)) {
            $response['message'] = '没有文件上传或服务器上传限制';
            return json($response, 200);
        }
        $upload = Config::get('upload');
        preg_match('/(\d+)(\w+)/', $upload['maxsize'], $matches);
        $type = strtolower($matches[2]);
        $typeDict = ['b' => 0, 'k' => 1, 'kb' => 1, 'm' => 2, 'mb' => 2, 'gb' => 3, 'g' => 3];
        $size = (int)$upload['maxsize'] * pow(1024, isset($typeDict[$type]) ? $typeDict[$type] : 0);
        $extensions = $upload['extensions'] ?? '';
        //文件后缀
        $suffix = $file->getOriginalExtension();
        $suffix = $suffix && preg_match("/^[a-zA-Z0-9]+$/", $suffix) ? $suffix : 'file';
        $mimeType = $file->getOriginalMime();
        $fileSize = $file->getSize();//未知
        //禁止上传PHP和HTML文件
        if (in_array($mimeType, ['text/x-php', 'text/html']) || in_array($suffix, ['php', 'html', 'htm'])) {
            $response['message'] = '上传的文件格式受到限制';
            return json($response, 200);
        }
        $filesystem = Config::get('filesystem');
        $rootPath = $filesystem['disks']['editor']['url'];
        //验证文件后缀
        if ($extensions !== '*' && !in_array($suffix, explode(',', $extensions))) {
            $response['message'] = '上传的文件格式受到限制';
            return json($response, 200);
        }
        try {
            $savename = Filesystem::disk('editor')->putFile($dir, $file);
        } catch (ValidateException $e) {
            $response['message'] = $e->getMessage();
            return json($response, 200);
        }
        unset($response);
        $response['error'] = 0;
        $savePath = $rootPath . '/' . $savename;
        $response['url'] = $savePath;
        return json($response,200);
    }

}